from threading import Thread
from urllib.parse import urlparse, parse_qs

from fake_useragent import UserAgent
import requests
from time import sleep
from queue import Queue

class MyThread(Thread):
    def __init__(self):
        Thread.__init__(self)
    def run(self):
        while not url_queue.empty():
            url = url_queue.get()
            headers = {'User-Agent':UserAgent().chrome}
            # 导入参数
            query = urlparse(url).query  # 提取参数部分 pageNo=1&pageSize=50
            params = parse_qs(query)  # 转成字典 {'pageNo': ['1'], 'pageSize': ['50']}
            page_no = params["pageNo"][0]  # 取出 '1'
            print(url)
            resp = requests.get(url, headers=headers)
            print(url)
            resp = requests.get(url,headers = headers)
            # 处理响应结果
            # for d in resp.json().get('data'):
            #     print(f'tid:{d.get("tid")} topic:{d.get("topicName")} content:{d.get("content")}')
            sleep(3)
            if resp.status_code == 200:
                print(f'成功获取第{page_no}页数据')


if __name__ == '__main__':
    url_queue = Queue()
    for i in range(1,11):
        url = f'https://www.hupu.com/home/v1/news?pageNo={i}&pageSize=50'
        url_queue.put(url)

    for i in range(3):
        t1 = MyThread()
        t1.start()
   